describe('User Create', ()->
    location=null;
    beforeEach(()->
        location= document.location.pathname;
    )
    afterEach(()->
        Backbone.history.navigate(location)
        Backbone.history.stop();
        Loading.Stop();
    );
    it('Create view should redirect for listing where get success message', ()->
        spyOn(AdminUserRoutes.prototype, "Listing")
        router = new AdminUserRoutes();
        model = new AdminUserCreateModel()
        view = new AdminUserCreateView({ model: model })
        Backbone.history.start({silent: true, pushState: true});
        server = sinon.fakeServer.create();
        server.respondWith("POST",
          SiteMap.Admin.Users.Create,
          [200, { "Content-Type": "application/json" },
          '{"SuccessMessage":"Success"}']);

        view.save({preventDefault:()->return});
        server.respond();
        expect(document.location.pathname).toEqual(SiteMap.Admin.Users.Listing)

        server.restore();
        return
    );

    it("Create view shouldn't redirect for listing where get error message", ()->
        spyOn(AdminUserRoutes.prototype, "Listing")
        router = new AdminUserRoutes();
        
        model = new AdminUserCreateModel()
        view = new AdminUserCreateView({ model: model })
        Backbone.history.start({silent: true, pushState: true});
        router.navigate(SiteMap.Admin.Users.Create, false)
        server = sinon.fakeServer.create();
        server.respondWith("POST",
          SiteMap.Admin.Users.Create,
          [200, { "Content-Type": "application/json" },
          '{"Errors":[{"Message":"value"},{"Message":"value"}],"SuccessMessage":""}}']);

        view.save({preventDefault:()->return});
        server.respond();
        expect(document.location.pathname).toEqual(SiteMap.Admin.Users.Create)

        server.restore();
        return
    );
);

describe('User Edit', ()->
    location=null;
    beforeEach(()->
        location= document.location.pathname;
        $("body").append("<div id='content'></div>")
    )
    afterEach(()->
        Backbone.history.navigate(location)
        Backbone.history.stop();
        Loading.Stop();
    );
    it('Create view should redirect for listing where get success message', ()->
        spyOn(AdminUserRoutes.prototype, "Listing")
        server = sinon.fakeServer.create();
        router = new AdminUserRoutes();
        model = new AdminUserEditModel()
        model.set("UserId",1)
        view = new AdminUserEditView({ model: model })
        Backbone.history.start({silent: true, pushState: true});
        server.respondWith("POST",
          SiteMap.Admin.Users.Edit+"?userId=1",
          [200, { "Content-Type": "application/json" },
          '{"SuccessMessage":"Success"}']);

        view.save({preventDefault:()->return});
        server.respond();
        expect(document.location.pathname).toEqual(SiteMap.Admin.Users.Listing)

        server.restore();
        return
    );

    it("Create view shouldn't redirect for listing where get error message", ()->
        spyOn(AdminUserRoutes.prototype, "Listing")
        router = new AdminUserRoutes();
        Backbone.history.start({silent: true, pushState: true});
        router.navigate(SiteMap.Admin.Users.Edit, false)
        server = sinon.fakeServer.create()
        model = new AdminUserCreateModel()
        model.set("UserId",1)
        view = new AdminUserCreateView({ model: model })
       
        
        server.respondWith("POST",
          SiteMap.Admin.Users.Edit+"?userId=1",
          [200, { "Content-Type": "application/json" },
          '{"Errors":[{"Message":"value"},{"Message":"value"}],"SuccessMessage":""}}']);

        view.save({preventDefault:()->return});
        server.respond();
        expect(document.location.pathname).toEqual(SiteMap.Admin.Users.Edit)

        server.restore();
        return
    );

    it("Edit view should exec render", ()->
        server = sinon.fakeServer.create()
        model = new AdminUserEditModel()
        model.set("UserId",1)
        spyOn(AdminUserEditView.prototype, "render")
        view = new AdminUserEditView({ model: model })
              
        server.respondWith("GET",
          SiteMap.Admin.Users.Edit+"?userId=1",
          [200, { "Content-Type": "application/json" },
          '{ "Email":"megaEmail","Login":"megaLogin","Status": 2, "Avatar":"TestAvatar"}']);

        server.respond();
        expect(AdminUserEditView.prototype.render).toHaveBeenCalled()
        server.restore();
        return
    );

    it("Listing view should exec render", ()->
        server = sinon.fakeServer.create()
        model = new AdminUserListingModel()
        model.set("Page",1);
        model.set("Size",10);
        model.set("Column","Email");
        model.set("Direction",1);
        model.set("Filter":{"Email": "TestEmail", "Login":"TestLogin"})
        spyOn(AdminUserListingView.prototype, "render")
        view = new AdminUserListingView({ model: model })
              
        server.respondWith("GET",
              SiteMap.Admin.Users.Listing+"?page=1&size=10&Column=Email&Direction=1&Filter.Email=TestEmail&Filter.Login=TestLogin",
              [200, { "Content-Type": "application/json" },
              '{ "Filter":{"Email":"megaEmail","Login":"megaLogin"},"Page": 2, "Size":15, "TotalPages": 10,"Direction":0,"Column":"Login","Users":[{"UserId":1}, {"UserId":2}]}']);

        server.respond();
        expect(AdminUserListingView.prototype.render).toHaveBeenCalled()
        server.restore();
        return
    );
);